/*********************************************************
*  @file    cm_uart_demo.c
*  @brief   ML302 OpenCPU uart APIs example
*  Copyright (c) 2019 China Mobile IOT.
*  All rights reserved.
*  created by XieGangLiang 2019/10/17
********************************************************/

#include "cm_main.h"
#include "atol_all_head.h"


/*----------------------------------------------------------------------------------------------------
# 函数功能 数据定义
# 参数介绍
----------------------------------------------------------------------------------------------------*/
#define OPENCPU_COMM_URAT CM_UART_1

osThreadId OC_Uart1_TaskHandle;

unsigned int uart1_data_arrived = 0;
unsigned char uart1_buf[1024];
int uart1_buf_len = 0;

osMutexDef( uart1_cmdMutex );
osMutexId uart1_cmd_mutex;

unsigned char * uart1_cmd_buf[20]; //最多接受20个参数
int uart1_cmd_len;

/*----------------------------------------------------------------------------------------------------
# 函数功能 uart1的接收中断，接收到数据时更新标志位
# 参数介绍
----------------------------------------------------------------------------------------------------*/
static void atol_oc_uar1_cb(void *param,unsigned int evt) {
   uart1_data_arrived = 1;
}


/*----------------------------------------------------------------------------------------------------
# 函数功能 uart1的接收方法，接收数据都在其中处理
# 参数介绍
----------------------------------------------------------------------------------------------------*/
static atol_firmware_uart1_recv_task(void *param)
{
    unsigned char buf[64] = {0};
    unsigned char * p;
    int len ;
	
    while(true)
    {
        if(uart1_data_arrived == 1)
        {             
            uart1_buf_len = cm_uart_receive(OPENCPU_COMM_URAT,uart1_buf,1024);
            
            while(uart1_buf_len < 1024)
            {
                len = cm_uart_receive(OPENCPU_COMM_URAT,uart1_buf+uart1_buf_len,1024-uart1_buf_len);
                
                if(len == 0)
                    break;
                uart1_buf_len += len;
            } 
            
            // if(osMutexWait( uart1_cmd_mutex, 0 ) != 0)
            // {
                // cm_printf("busy\n");
                // uart1_buf_len = 0;
                // uart1_data_arrived = 0;
                // continue;
            // }
            // if(cm_cmd_engine("CM",uart1_buf,&uart1_buf_len,uart1_cmd_buf,&uart1_cmd_len) == -1)
            // {
                // cm_printf("error\n");
                // uart1_buf_len = 0;
                // osMutexRelease( uart1_cmd_mutex );
            // }
            // else
            // {
                // uart1_buf_len = 0;
                // osSignalSet(OC_Main_TaskHandle, 0x0004);   
            // }
          
            uart1_data_arrived = 0;
        }
        osDelay(20);   
    }
}


// /*----------------------------------------------------------------------------------------------------
// # 函数功能 uart1的打印方法，一般默认使用uart0的
// # 参数介绍
// ----------------------------------------------------------------------------------------------------*/
// void atol_firmware_uart1_printf (char *str, ...) {
    // static  char s[600]; //This needs to be large enough to store the string TODO Change magic number
    // va_list args;
    // int len;
    // if ((str == NULL) || (strlen(str) == 0))
    // {
        // return;
    // }
    // va_start (args, str);
    // len = vsprintf ((char*)s, str, args);
    // va_end (args);
    // cm_uart_send_no_cache(CM_UART_1,s,len,100);
// }


/*----------------------------------------------------------------------------------------------------
# 函数功能 uart1的初始化方法，只是硬件的初始化
# 参数介绍
----------------------------------------------------------------------------------------------------*/
void atol_firmware_uart1_init()
{
    cm_uart_cfg_t cfg;
    
    // 不需要互锁功能
    osThreadDef(OC_Uart1_Task, atol_firmware_uart1_recv_task, osPriorityNormal, 0, 1024);
    OC_Uart1_TaskHandle = osThreadCreate(osThread(OC_Uart1_Task), 0);
    uart1_cmd_mutex = osMutexCreate( osMutex(uart1_cmdMutex) );

	// 初始化UART1的
    cfg.id = OPENCPU_COMM_URAT;
    cfg.baud = CM_UART_BAUD_115200;
    cfg.databit = CM_UART_DATA_BITS_8;
    cfg.stopbit = CM_UART_STOP_BITS_1;
    cfg.parity = CM_UART_NO_PARITY;
	
	// 接收数据标记函数
    cfg.cb = atol_oc_uar1_cb;
    cfg.param = "uart1 param";
    cfg.event = CM_UART_EVENT_RX_ARRIVED;
	
    cfg.rx_buf_size = 4*1024;
    cfg.tx_buf_size = 4*1024;
	
    cm_uart_init(&cfg);
    
}